package com.jxs.android.weiget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jxs.android.util.StringUtils;
import com.jxs.android.weixi.data.AccountInfo;
import com.telecom.weixi.R;

public class BuAccountInfoPageView extends LinearLayout {
	static public interface LoadingDataListener {
		public void loadingData(int currentPage);
	}

	private Context context;
	private Button curt_index, acc_nbr, cur_mon, lst_mon, last_mon;
	TextView null_has_data;
	private ListView listView;
	private int currentPage = 1;
	private LayoutInflater inflater;
	private AccountInfoAdapter adapter;

	private View loadMoreView;
	private Button loadMoreButton;
	private LoadingDataListener dataListener;

	public BuAccountInfoPageView(Context context) {
		super(context);
		this.context = context;
		initView();
	}

	private void initView() {
		inflater = LayoutInflater.from(context);
		inflater.inflate(R.layout.bu_tab_business, this);
		curt_index = (Button) findViewById(R.id.curt_index);
		acc_nbr = (Button) findViewById(R.id.cust_manager_name);
		cur_mon = (Button) findViewById(R.id.cur_mon);
		lst_mon = (Button) findViewById(R.id.lst_mon);
		last_mon = (Button) findViewById(R.id.last_mon);
		null_has_data = (TextView) findViewById(R.id.null_has_data);

		curt_index.setTextColor(context.getResources().getColor(R.color.white));
		acc_nbr.setTextColor(context.getResources().getColor(R.color.white));
		cur_mon.setTextColor(context.getResources().getColor(R.color.white));
		lst_mon.setTextColor(context.getResources().getColor(R.color.white));
		last_mon.setTextColor(context.getResources().getColor(R.color.white));

		acc_nbr.setSingleLine(false);
		cur_mon.setSingleLine(false);
		lst_mon.setSingleLine(false);
		last_mon.setSingleLine(false);
		loadMoreView = inflater.inflate(R.layout.bu_loading_btn, null);
		loadMoreButton = (Button) loadMoreView.findViewById(R.id.loadMoreButton);
		loadMoreButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				loadMoreButton.setText("加载中..."); // ���ð�ť����
				if (null != dataListener) {
					dataListener.loadingData(currentPage);
				}
			}
		});

		listView = (ListView) findViewById(R.id.common_listview);
		listView.addFooterView(loadMoreView);
		adapter = new AccountInfoAdapter();
		listView.setAdapter(adapter);

	}

	public void setDataListener(LoadingDataListener dataListener) {
		this.dataListener = dataListener;
	}

	public AccountInfoAdapter getAdapter() {
		return adapter;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setEmpty(String text) {
		null_has_data.setText(text);
		null_has_data.setVisibility(View.VISIBLE);
	}

	public void setEmptyVisibility(int visibility) {
		null_has_data.setVisibility(visibility);
	}

	public void initTitle(String acc_nbr, String cur_mon, String lst_mon, String last_mon) {
		this.acc_nbr.setText(acc_nbr);
		this.cur_mon.setText(cur_mon);
		this.lst_mon.setText(lst_mon);
		this.last_mon.setText(last_mon);
	}

	public void setAccountInfos(List<AccountInfo> custInfos) {
		adapter.setAccountInfos(custInfos);
		adapter.notifyDataSetChanged();
	}

	private class AccountInfoAdapter extends BaseAdapter {

		private List<AccountInfo> accountInfos = new ArrayList<AccountInfo>();

		private ViewHolder holder = null;

		public AccountInfoAdapter() {
		}

		public void setAccountInfos(List<AccountInfo> custInfos) {
			if (currentPage == 1) {
				this.accountInfos.clear();
			}
			null_has_data.setVisibility(View.GONE);
			this.accountInfos.addAll(custInfos);
		}

		@Override
		public int getCount() {
			return accountInfos.size();
		}

		@Override
		public AccountInfo getItem(int position) {
			return accountInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			AccountInfo custInfo = accountInfos.get(position);

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.bu_tab_business_item, null);
				holder = new ViewHolder();
				holder.curt_index = (Button) convertView.findViewById(R.id.curt_index);
				holder.acc_nbr = (Button) convertView.findViewById(R.id.cust_manager_name);
				holder.cur_mon = (Button) convertView.findViewById(R.id.cur_mon);
				holder.lst_mon = (Button) convertView.findViewById(R.id.lst_mon);
				holder.last_mon = (Button) convertView.findViewById(R.id.last_mon);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if (!StringUtils.isEmpety(custInfo.getCust_manager_name())) {
				holder.curt_index.setText((position + 1) + "");
			} else {
				holder.curt_index.setText("");
			}
			holder.acc_nbr.setText(custInfo.getCust_manager_name());
			holder.cur_mon.setText(custInfo.getCur_mon());
			holder.lst_mon.setText(custInfo.getLst_mon());
			holder.last_mon.setText(custInfo.getLast_mon());

			int resid = R.drawable.table_row1;
			/*
			 * if (position % 2 == 0) { resid = R.drawable.table_row0; }
			 */
			holder.curt_index.setBackgroundResource(R.drawable.table_row_index_1);
			holder.acc_nbr.setBackgroundResource(resid);
			holder.cur_mon.setBackgroundResource(resid);
			holder.lst_mon.setBackgroundResource(resid);
			holder.last_mon.setBackgroundResource(resid);
			return convertView;
		}

		class ViewHolder {
			Button curt_index;// �ͻ�����
			Button acc_nbr;// �ͻ�����
			Button cur_mon;// �ͻ�ARPU
			Button lst_mon;// ��Ʒ��Ϣ
			Button last_mon;// ������Ϣ
		}

	}

	public View getLoadMoreView() {
		return loadMoreView;
	}

	public Button getLoadMoreButton() {
		return loadMoreButton;
	}

}
